package com.example.zsfinacialmanage.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;

public class MyDBHelper extends SQLiteOpenHelper {

    //定义数据库名称
    private static final String DBNAME = "financial.db";
    //定义数据库版本号
    private static final int VERSION = 1;
    public MyDBHelper(@Nullable Context context) {
        super(context, DBNAME,null,VERSION);
    }
    //创建数据库
    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建用户表
        // TODO 用户名字段唯一（账号只能唯一）
        db.execSQL("create table tb_userinfo(id integer primary key autoincrement,name varchar(20) unique,pwd varchar(20),email varchar(50),phone varchar(11))");
        //添加默认用户root，默认都为root
        ContentValues values = new ContentValues();
        values.put("name","root");
        values.put("pwd",Md5Util.get("root"));
        values.put("email","root");
        values.put("phone","root");
        db.insert("tb_userinfo",null,values);
        //创建收入表
        db.execSQL("create table in_come(id integer primary key autoincrement,name varchar(20),inmoney double,intime varchar(20),intype varchar(30),inpayer varchar(100),inremark varchar(500))");
        //创建支出表
        db.execSQL("create table pay_out(id integer primary key autoincrement,name varchar(20),outmoney double,outtime varchar(20),outtype varchar(30),outpayee varchar(100),outremark varchar(500))");
    }
    //升级数据库
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}
